//
//  ByUIShopsIndexVC.m
//  chequanbao_ios
//
//  Created by pro on 17/5/6.
//  Copyright © 2017年 com.itboye. All rights reserved.
//

#import "ByUIShopsIndexVC.h"

// vc
#import "ByUIShopsDetailVC.h"           // 商家详情
#import "JKShopsDetailVC.h"             // 详情新
#import "ByUIIndexLocationVC.h"         // 定位

// cell
#import "ByCellShopsIndexShopsItem.h"

// api
#import "ByApiShops.h"

// model
#import "ByModelShopsList.h"

@interface ByUIShopsIndexVC () <UITableViewDelegate, UITableViewDataSource, UISearchBarDelegate>

@property (nonatomic, strong) UITableView *table;
@property (nonatomic, strong) UIView *locationView;  // 定位

@property (nonatomic, strong) NSMutableArray *items;

@end

@implementation ByUIShopsIndexVC
{
    
    NSInteger _page;
    BOOL _showDistance; // 如果定位城市和选择城市相同，则显示距离，反之不显示
}

- (void)viewDidLoad {
    [super viewDidLoad];

    [self addLeftBackBarButtonItem];
//    [self createCityLocationView];
    
    self.title = @"商家";
    
    [self createNaviItems];
    [self createTableView];
    
    // 请求附近商家
    [self.table.mj_header beginRefreshing];
}
- (void)viewWillAppear:(BOOL)animated {
    
    [super viewWillAppear:animated];
    
    [self setNaviAttributeWithTranslucent:YES stateBarLightContent:YES];
}

#pragma mark - 懒加载
- (NSMutableArray *)items {
    
    if (!_items) {
        _items = [NSMutableArray new];
    }
    return _items;
}

#pragma mark - 请求数据
- (void)queryShopsNearby {
    
    ByModelLocationCity *selectedCityModel = BY_APPDELEGATE.selectedCityModel;
    // 判断定位城市和选择城市是否相同
    if (selectedCityModel == nil) {
        _showDistance = YES;
    } else {
        if ([selectedCityModel.city isEqualToString:BY_APPDELEGATE.locationCity]) {
            _showDistance = YES;
        } else {
            _showDistance = NO;
        }
    }
    
    
    ByNetParamsModel *params = [ByNetParamsModel new];
    NSLog(@"%@", BY_APPDELEGATE.locationDic);
    
    
    
    if (selectedCityModel) {
        params.city_code = selectedCityModel.cityID;
    }else{
        params.city_code = BY_APPDELEGATE.locationDic[@"CityID"];
     
    }
    if (_searchName.length != 0) {
        params.name = _searchName;
    }
    if (self.cate_id) {
        params.cate_id = self.cate_id;
    }
    params.lng = BY_APPDELEGATE.locationDic[@"lng"];
    params.lat = BY_APPDELEGATE.locationDic[@"lat"];
    params.page_index = [NSString stringWithFormat:@"%li", _page];
    params.page_size = @"10";
    
//    [self performSelector:@selector(loading) withObject:nil afterDelay:0.01];
    
    
    
    [ByApiShops queryShopsNearby:params success:^(ByNetBaseResult *result) {
        
        [ByCoreToolAlert hideHud];

        ByModelShopsList *listModel = result.data;
        
        if (_page <= 1) {
            
            [self.items removeAllObjects];
        }
        
        [self.items addObjectsFromArray:listModel.list];
        [self.table reloadData];
        
        if (self.items.count == listModel.count.integerValue) {
            
            [self.table.mj_header endRefreshing];
            [self.table.mj_footer endRefreshingWithNoMoreData];
        } else {
            
            [self.table.mj_header endRefreshing];
            [self.table.mj_footer endRefreshing];
        }
    
    } failure:^(ByNetBaseResult *result) {
        
        [ByCoreToolAlert hideHud];
        [self.table.mj_header endRefreshing];
        [self.table.mj_footer endRefreshingWithNoMoreData];
        
    }];
}
//- (void)loading {
//    
//    [ByCoreToolAlert showLoadind:@"正在加载..."];
//}

#pragma mark - create views
- (void)createNaviItems {
    
    UIView *titleView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH * 0.7, 30)];
    
    UISearchBar *searchBar = [[UISearchBar alloc] initWithFrame:titleView.bounds];
    searchBar.placeholder = @"搜索";
    searchBar.backgroundColor = [UIColor clearColor];
    searchBar.layer.cornerRadius = 15;
    searchBar.layer.masksToBounds = YES;
    searchBar.delegate = self;
    if (self.searchName) {
        searchBar.text = self.searchName;
    }
    [titleView addSubview:searchBar];

    self.navigationItem.titleView = titleView;
}
// 创建导航栏视图
- (void)createCityLocationView {
    
    self.locationView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH * 0.15, 30)];
    self.locationView.backgroundColor = [UIColor clearColor];
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(locationTap:)];
    [self.locationView addGestureRecognizer:tap];
    
    UIImageView *downIcon = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 10, 10)];
    downIcon.image = [UIImage imageNamed:@"Index_jiantou_down"];
    [self.locationView addSubview:downIcon];
    downIcon.right = self.locationView.width - 10;
    downIcon.y = self.locationView.height / 2;
    downIcon.tag = 1;
    
    UILabel *cityLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, downIcon.left, self.locationView.height)];
    cityLabel.font = FONT(14);
    cityLabel.text = @"选择";
    cityLabel.textColor = [UIColor whiteColor];
    cityLabel.textAlignment = NSTextAlignmentCenter;
    [self.locationView addSubview:cityLabel];
    cityLabel.tag = 2;
    
    [self addCustomItemviews:@[self.locationView] position:ByNavigationItemPositionModeRight];
}
// 创建表视图
- (void)createTableView {
    
    self.table = [[UITableView alloc] initWithFrame:CGRectMake(10, 64, SCREEN_WIDTH - 20, SCREEN_HEIGHT - 64) style:UITableViewStylePlain];
    self.table.backgroundColor = [UIColor clearColor];
    self.table.delegate = self;
    self.table.dataSource = self;
    [self.view addSubview:self.table];
    
    
    self.table.tableFooterView = [UIView new];
    
    ByCoreRefreshGifHeader *header = [ByCoreRefreshGifHeader headerWithRefreshingBlock:^{
        
        _page = 1;
        
        [self queryShopsNearby];
    }];
    
    ByCoreRefreshGifFooter *footer = [ByCoreRefreshGifFooter footerWithRefreshingBlock:^{
        
        _page ++;
        
        [self queryShopsNearby];
    }];
    
    self.table.mj_header = header;
    self.table.mj_footer = footer;

}

#pragma mark - search bar delegate 
- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText {
    
    _searchName = searchText;
    [self.table.mj_header beginRefreshing];
}

#pragma mark - UITableViewDataSource UITableViewDelegate
// 设置组数
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return self.items.count;
}
// 设置行数
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 1;
}
// 设置单元格属性
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    ByCellShopsIndexShopsItem *cell = [tableView dequeueReusableCellWithIdentifier:@"ByCellShopsIndexShopsItem"];
    if (cell == nil) {
        cell = [[ByCellShopsIndexShopsItem alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"ByCellShopsIndexShopsItem"];
        cell.showDistance = YES;
    }
    
    cell.model = self.items[indexPath.section];
    
    return cell;
    
}
// 设置单元格行高
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 100;
}

// 设置组尾高
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return 10;
}
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    
    return [UIView new];
}

// 单元格点击事件
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
 
//    ByUIShopsDetailVC *vc = [[ByUIShopsDetailVC alloc] init];
//    vc.shopsItemModel = self.items[indexPath.section];
//    [self by_normalPush:vc hasNavi:YES];
    
    JKShopsDetailVC *vc = [[JKShopsDetailVC alloc] init];
    vc.shopsItemModel = self.items[indexPath.section];
    [self by_normalPush:vc hasNavi:YES];
}
//- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {

//    cell.alpha = 0.6;
//    CATransform3D t = CATransform3DIdentity;
//    t.m34 = -1 / 500.0;
//    t = CATransform3DTranslate(t, 0, 0, 50);
//    cell.layer.transform = t;
//    [UIView animateWithDuration:0.5 animations:^{
//        cell.alpha = 1;
//        cell.layer.transform = CATransform3DIdentity;
//    }];
    
//    cell.transform = CGAffineTransformMakeTranslation(-100, 0);
//    
//    [UIView animateWithDuration:0.3 animations:^{
//        cell.transform = CGAffineTransformIdentity;
//    }];
//}
- (void)locationTap:(UITapGestureRecognizer *)tap {
    
    ByUIIndexLocationVC *vc = [[ByUIIndexLocationVC alloc] init];
    [self by_normalPush:vc hasNavi:YES];
    
}


@end
